package com.raysweather.android;

import java.io.InputStream;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class Map extends Activity {

	private Timer timer;
	private int updateDelay = 15;
	private RaysWeather rw;
	private static Map map;
	private DataPoller d;
	BroadcastReceiver bReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(action.equalsIgnoreCase("update")){    
				update();
			}
		}
	};


	private String location = "";
	private Drawable radar, county, highway, city, legend;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.radar);
		
		/* Get closest radar */
		rw = RaysWeather.getInstance();
		registerReceiver(bReceiver, new IntentFilter("update"));
		d = rw.getService();
		if(d != null){
			update();
			Log.e("RADAR", d.getClosestRadar()+"A");
		}

		//		radar=LoadImageFromWebOperations("http://raysweather.com/radar_images/GSP_N0R_0.gif");
		//		county=LoadImageFromWebOperations("http://raysweather.com/radar_images/GSP_County_Short.gif");
		//		highway=LoadImageFromWebOperations("http://raysweather.com/radar_images/GSP_Highways_Short.gif");
		//		city=LoadImageFromWebOperations("http://raysweather.com/radar_images/GSP_City_Short.gif");
		//		legend=LoadImageFromWebOperations("http://raysweather.com/radar_images/GSP_N0R_Legend_0.gif");
		//		radar=LoadImageFromWebOperations("http://raysweather.com/radar_images/N0R_"+location+"_loop.gif");
		//		
		//		((ImageView)findViewById(R.id.radar_image)).setImageDrawable(radar);
		//		((ImageView)findViewById(R.id.county_image)).setImageDrawable(county);
		//		((ImageView)findViewById(R.id.highway_image)).setImageDrawable(highway);
		//		((ImageView)findViewById(R.id.city_image)).setImageDrawable(city);
		//		((ImageView)findViewById(R.id.legend_image)).setImageDrawable(legend);
	}

	private Drawable LoadImageFromWebOperations(String url)
	{
		try
		{
			InputStream is = (InputStream) new URL(url).getContent();
			Drawable d = Drawable.createFromStream(is, "src name");
			return d;
		}catch (Exception e) {
			System.out.println("Exc="+e);
			return null;
		}
	}

	private void update(){
		d = rw.getService();
		if(d != null){
			location = d.getClosestRadar();
			Log.e("RADAR", location);
			radar=LoadImageFromWebOperations("http://raysweather.com/radar_images/N0R_"+location+"_loop.gif");

			((ImageView)findViewById(R.id.radar_image)).setImageDrawable(radar);
		}
	}

}